package main

import (
	"fmt"
	"net/http"
	"pro_cla_sql/control"
	"pro_cla_sql/model"
	"pro_cla_sql/util"

	jwt "github.com/dgrijalva/jwt-go"
)

func mid(next http.HandlerFunc) http.HandlerFunc {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		r.ParseForm()
		token := r.Form.Get("token")
		if token == "" {
			w.Write(util.NewResult(util.Fail, "没有token,请重试"))
			return
		}
		d1 := &model.Jwt{}
		j, err := jwt.ParseWithClaims(token, d1, func(token *jwt.Token) (interface{}, error) {
			return []byte("k"), nil
		})
		if err != nil {
			w.Write(util.NewResult(util.Fail, "token 非法,请重试"))
			return
		}
		// 合法
		if j.Valid {
			next.ServeHTTP(w, r)
		} else { // 不合法
			w.Write(util.NewResult(util.Fail, "不合法,请重试"))
			return
		}
		//
	})
}

func main() {
	http.HandleFunc(`/login`, control.Login)
	http.HandleFunc(`/newslist`, control.Cnotice)
	http.HandleFunc(`/ruleslist`, control.Crules)
	http.HandleFunc(`/infoslist`, control.Sinfos)
	http.HandleFunc(`/cinfoslist`, control.Cinfos)
	http.HandleFunc(`/cimgslist`, control.Cimgs)
	http.HandleFunc(`/courseslist`, control.Course)
	http.HandleFunc(`/sapplyslist`, control.Sapply)
	http.HandleFunc(`/sleaveslist`, control.Sleave)
	http.HandleFunc(`/sworkslist`, control.Swork)
	http.HandleFunc(`/activitylist`, control.Activity)
	http.HandleFunc(`/sinfoone`, control.SinfosOne)

	http.HandleFunc(`/sleavesadd`, control.Sleavesadd)
	http.HandleFunc(`/competitonlist`, control.Competiton)
	http.HandleFunc(`/sapplysadd`, control.Sapplysadd)
	http.HandleFunc(`/sworksadd`, control.Sworksadd)
	http.HandleFunc(`/updateinfo`, control.Sinfoupdate)
	http.HandleFunc(`/cifoone`, control.Cifoone)
	http.HandleFunc(`/up.html`, control.Up)
	http.HandleFunc(`/file`, control.File)

	fmt.Println("app is run on 80")
	http.ListenAndServe(":80", nil)
}
